package com.wn.icloud.provider.uac.service.app;

import com.wn.icloud.biz.BaseBiz;
import com.wn.icloud.common.context.BaseContextHandler;
import com.wn.icloud.provider.uac.entity.Group;
import com.wn.icloud.provider.uac.entity.User;
import com.wn.icloud.provider.uac.mapper.GroupMapper;
import com.wn.icloud.provider.uac.mapper.UserMapper;
import com.wn.icloud.provider.uac.vo.app.AppUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class AppUserBiz extends BaseBiz<UserMapper, User>{

    @Autowired
    private GroupMapper groupMapper;

    public AppUserDetails getUserDetailByCurrentUserName() {
        AppUserDetails appUserDetails = new AppUserDetails();
        String currentUserName = BaseContextHandler.getUsername();
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("username", currentUserName);
        List<User> users = this.selectByExample(example);
        for(User u : users){
            if (u.getAddress()==null){

            }
            appUserDetails.setAddress(u.getAddress());
            appUserDetails.setBirthday(u.getBirthday());
            appUserDetails.setDescription(u.getDescription());
            appUserDetails.setEmail(u.getEmail());
            appUserDetails.setMobilePhone(u.getMobilePhone());
            appUserDetails.setName(u.getName());
            appUserDetails.setSex(u.getSex());
            appUserDetails.setTelPhone(u.getTelPhone());
            appUserDetails.setType(u.getType());
            appUserDetails.setUsername(u.getUsername());
            List<String>  roleList = new ArrayList<>();
            List<String> workshopList = new ArrayList<>();
            // 根据userId获取角色及部门(群主|领导)
            List<Group> groups = groupMapper.selectGroupByUserId(u.getId().toString());
            groups.forEach(group -> {
                if (group.getGroupType() ==1){
                    roleList.add(group.getName()+"(群主|领导)");
                }
                if (group.getGroupType() ==5){
                    workshopList.add(group.getName());
                }
            });
            // 根据userId获取角色及部门(组员|下属)
            List<Group> groupList = groupMapper.selectGroupMemberByUserId("11");
            groupList.forEach(group -> {
                if (group.getGroupType() ==1){
                    roleList.add(group.getName()+"(组员|下属)");
                }
                if (group.getGroupType() ==5){
                    workshopList.add(group.getName());
                }
            });
            String role = String.join("/", roleList);
            String workshop = String.join("/", workshopList);
            appUserDetails.setRole(role);
            appUserDetails.setWorkshop(workshop);
            if (appUserDetails.getAddress()==null){
                appUserDetails.setAddress("未填写");
            }
            if (appUserDetails.getBirthday()==null){
                appUserDetails.setBirthday("未填写");
            }
            if (appUserDetails.getDescription()==null){
                appUserDetails.setDescription("未填写");
            }
            if (appUserDetails.getEmail()==null){
                appUserDetails.setEmail("未填写");
            }
            if (appUserDetails.getMobilePhone()==null){
                appUserDetails.setMobilePhone("未填写");
            }
            if (appUserDetails.getSex()==null){
                appUserDetails.setSex("未填写");
            }
            if (appUserDetails.getTelPhone()==null){
                appUserDetails.setTelPhone("未填写");
            }
            if (appUserDetails.getType()==null){
                appUserDetails.setType("未填写");
            }
            if (appUserDetails.getRole()==null){
                appUserDetails.setRole("未设置");
            }
            if (appUserDetails.getWorkshop()==null){
                appUserDetails.setWorkshop("未设置");
            }
        }

        return appUserDetails;
    }
}
